import function.DataBase;
import function.MathematicsExamination;


import java.io.IOException;
import java.util.Scanner;


/**
 * @version 1.0
 * @program: math test
 * @description: 主程序
 * @author: Kishan Liu
 * @create: 2021-12-09 17:12
 **/
public class MainAndMenu {
    public static void main(String[] args) throws IOException {
        Menu.welcomeMenu();
        Menu.controlMenu(Menu.logMenu());
    }




    /**
     * @version 1.0
     * @program: math test
     * @description: 菜单
     * @author: kishan Liu
     * @create: 2021-12-09 16:24
     **/
    public static class Menu {

        //接收用户选择
        static private int scanSelect(){
            int select=0;
            System.out.println("*********************************");
            System.out.print("您的选择是:");
            do {
                Scanner in = new Scanner(System.in);
                if (in.hasNext()){
                    select = in.nextInt();
                    if (select<1||select>4){
                        System.out.println("输入错误，请重新输入！");
                    }
                }else {
                    System.out.println("输入错误，请重新输入！");
                }
            }while (select<1||select>4);
            return select;
        }

        //首次调用主菜单
        static private int  mainMenuCFirst (){
            System.out.println("--------------主菜单---------------");
            System.out.println("请选择以下功能（输入编号并回车）：");
            System.out.println("1.测试计算。");
            System.out.println("2.历史数据打印。");
            System.out.println("3.退出。");
            return scanSelect();
        }

        //进行一次测试后的主菜单
        static private int  mainMenuContinue (){
            System.out.println("--------------主菜单---------------");
            System.out.println("请选择以下功能（输入编号并回车）：");
            System.out.println("1.测试计算。");
            System.out.println("2.本次测试数据。");
            System.out.println("3.历史数据打印。");
            System.out.println("4.退出。");
            return scanSelect();
        }

        //欢迎菜单
        static public void welcomeMenu(){
            System.out.println("                         ******欢迎使用本程序******");
            System.out.println("本程序为一个帮助小学生练习数学的程序，可以帮助小学生练习100以内的加、减、乘、除四种数学运算。");
        }

        //再见菜单
        static private void goodbyeMenu(int time){
            System.out.println("本次登陆您一共学习了"+time+"秒！");
            System.out.println("期待您的再次使用！再见~");
        }

        //控制菜单
        static public void controlMenu(String id) throws IOException {
            long startTime = System.currentTimeMillis();
            boolean flag=true;
            for (;;){
                if (flag) {
                    switch (Menu.mainMenuCFirst()) {
                        case 1 -> {
                            MathematicsExamination.question(id);
                            flag = false;
                        }
                        case 2 -> DataBase.readData();
                        case 3 -> {
                            long endTime = System.currentTimeMillis();
                            goodbyeMenu((int) (endTime - startTime) / 1000);
                            return;
                        }
                    }
                }else {
                    switch (Menu.mainMenuContinue()) {
                        case 1 -> MathematicsExamination.question(id);
                        case 2 -> DataBase.readLastData();
                        case 3 -> DataBase.readData();
                        case 4 -> {
                            long endTime = System.currentTimeMillis();
                            goodbyeMenu((int) (endTime - startTime) / 1000);
                            return;
                        }
                    }
                }
            }
        }

        //登陆菜单
        static public String logMenu(){
            System.out.println("---------------登录界面--------------");
            String id ;
            for (;;) {
                System.out.print("请输入您的用户ID号(6位)：");
                boolean number = false;
                boolean english = false;
                Scanner in = new Scanner(System.in);
                id= in.next();
                if (id.length()!=6){
                    System.out.println("注意：输入的ID中必须是6位，且包括数字和英文字母。");
                    continue;
                }
                for (int i=0;i<6;i++){
                    if (id.charAt(i)>=48&&id.charAt(i)<=57) {
                        number = true;
                    }
                    if (id.charAt(i)>=97&&id.charAt(i)<=122 || id.charAt(i)>=65&&id.charAt(i)<=90) {
                        english = true;
                    }
                }

                if (number && english) {
                    System.out.println("登陆成功！");
                    System.out.print("用户“");
                    for (int j = 0; j < 6; j++) {
                        System.out.print(id.charAt(j));
                    }
                    System.out.println("”欢迎您！");
                    break;
                } else {
                    System.out.println("注意：输入的ID中必须是6位，且包括数字和英文字母。");
                }
            }
            return id;
        }
    }
}



